package resources.Grafo;

import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;

class Main {
	public static void main(String[] argv) {
		Grafo r = new Grafo();
		Nodo r1 = new Nodo("R1");
		r.agregarNodo(r1);
		Nodo r2 = new Nodo("R2");
		r.agregarNodo(r2);
		Nodo r3 = new Nodo("R3");
		r.agregarNodo(r3);
		Nodo r4 = new Nodo("R4");
		r.agregarNodo(r4);
		Nodo ra = new Nodo("Ra");
		r.agregarNodo(ra);
		Nodo r7 = new Nodo("R7");
		r.agregarNodo(r7);
		Nodo rs = new Nodo("Rs");
		r.agregarNodo(rs);
		Nodo rw = new Nodo("Rw");
		r.agregarNodo(rw);

		r.conectar(r1, r2);
		r.conectar(r1, r4);
		r.conectar(r1, r3);
		// r.caminosAsString(r1);
		r.conectar(r1, ra);
		r.conectar(r2, r4);
		r.conectar(r4, r3);
		r.conectar(r4, rs);
		r.conectar(r4, r7);
		r.conectar(r3, ra);
		r.conectar(rs, rw);
		r.conectar(r7, rw);
		r.conectar(r7, rs);
		/*
		 * Vector red = r.arbolDeDescenso(r1); for(int i = 0; i<red.size();i++)
		 * { ((Red) red.elementAt(i)).caminosAsString(r1); }
		 */
		System.out.println("lista nodos:" + r.listaNodos());
		System.out.println("r contains r2?:" + r.buscarNodo(r2));

		// System.out.println("caminos de r1 a r2: "+caminos.size()+ caminos);
		System.out.println("enlacesPtoP: r1-->" + r.buscarAdyacentes(r1));
		System.out.println("enlacesPtoP: r2-->" + r.buscarAdyacentes(r2));
		System.out.println("enlacesPtoP: r3-->" + r.buscarAdyacentes(r3));
		System.out.println("enlacesPtoP: r4-->" + r.buscarAdyacentes(r4));
		System.out.println("enlacesPtoP: ra-->" + r.buscarAdyacentes(ra));
		System.out.println("enlacesPtoP: r7-->" + r.buscarAdyacentes(r7));
		System.out.println("enlacesPtoP: rs-->" + r.buscarAdyacentes(rs));
		System.out.println("enlacesPtoP: rw-->" + r.buscarAdyacentes(rw));
		Vector caminos = r.caminos(r1, r7);
		System.out.println("caminos r1,r7 :" + caminos);
		System.out.println("iguales r1,r1:" + r1.equals(r1));
		
		dibujarGrafo(r);
	}
	
	private static void dibujarGrafo(Grafo g){
		
		
		Color[] colores = {Color.red, Color.green, Color.blue, Color.yellow, Color.pink, Color.gray, Color.orange, Color.black, Color.magenta};
		
		GraphModel model = new DefaultGraphModel();
		GraphLayoutCache view = new GraphLayoutCache(model,new DefaultCellViewFactory());
		JGraph graph = new JGraph(model, view);
		DefaultGraphCell[] cells = new DefaultGraphCell[g.listaNodos().size()+ g.getEnlaces().size()];
		
		int cont=0;
		
		for(int i=0; i<g.listaNodos().size(); i++){
			
			cont++;
			
			if(cont>=4)
				cont=0;
						
			cells[i] = new DefaultGraphCell(((Nodo)g.listaNodos().elementAt(i)).valor().toString());
			
			if(cont<2){
				GraphConstants.setBounds(cells[i].getAttributes(), new Rectangle2D.Double(20 + (i*100) ,20,80,20));
				GraphConstants.setGradientColor(cells[i].getAttributes(),colores[1]);//sacar
			//	GraphConstants.setGradientColor(cells[i].getAttributes(),colores[g.listaNodos().elementAt(i).getColor()]);
				GraphConstants.setOpaque(cells[i].getAttributes(), true);
				DefaultPort port0 = new DefaultPort();
				cells[i].add(port0);
			}
			
			if(cont>=2 && cont<4){
				GraphConstants.setBounds(cells[i].getAttributes(), new Rectangle2D.Double(20 + (i*100) ,20 + (i*50),80,20));
				GraphConstants.setGradientColor(cells[i].getAttributes(),colores[1]);//sacar
		//		GraphConstants.setGradientColor(cells[i].getAttributes(),colores[g.listaNodos().elementAt(i).getColor()]);
				GraphConstants.setOpaque(cells[i].getAttributes(), true);
				DefaultPort port0 = new DefaultPort();
				cells[i].add(port0);
				
			}
		
		}
		
		int inicio=0;
		int destino=0;
		
		for(int i=0; i<g.getEnlaces().size(); i++){
			for(int j=0; j<g.listaNodos().size(); j++){
				if(((Enlace)g.getEnlaces().elementAt(i)).getInicio().equals(g.listaNodos().elementAt(j))){
					inicio = j;
				}
				if(((Enlace)g.getEnlaces().elementAt(i)).getDestino().equals(g.listaNodos().elementAt(j))){
					destino = j;
				}
			}
			
			DefaultEdge edge = new DefaultEdge();
			System.out.println("inicio: " + inicio);
			System.out.println("destino: " + destino);
			edge.setSource(cells[inicio].getChildAt(0));
			edge.setTarget(cells[destino].getChildAt(0));
			cells[g.listaNodos().size()+i]=edge;
			int arrow = GraphConstants.ARROW_CLASSIC;
			GraphConstants.setLineEnd(edge.getAttributes(), arrow);
			GraphConstants.setEndFill(edge.getAttributes(), true);
			
		}
		
		graph.getGraphLayoutCache().insert(cells);
		
		JFrame frame = new JFrame();
		frame.getContentPane().add(new JScrollPane(graph));
		frame.pack();
		frame.setVisible(true);
	}
}